package com.ruoyi.server.api.imguser.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.server.api.imguser.pojo.ImgUser;
import com.ruoyi.server.api.imguser.service.ImgUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author wsb
 * @Date 2024/10/22 8:38
 **/

@RestController
@RequestMapping("/users")
@CrossOrigin
public class ImgUserController extends BaseController {


    @Autowired
    ImgUserService userService;

    /**
     * @Author wsb
     * @Description 用户登录  新增手机号
     * @Date 2024/11/15 10:27
     * @Return * @param null
     **/

    @PostMapping("/login")
    public AjaxResult login(@RequestBody ImgUser user) {
        String userId = user.getUserId();
        ImgUser userById = userService.getImgUserById(userId);
        if (userById != null) {
            if (user.getPhone() != null) {
                ImgUser imgUser = userService.updatePhoneByUserId(user);
                return success(imgUser);
            }
            return success(userById);
        } else {
            user.setNickName("手作大师");
            userService.insertImgUser(user);
            return success(user);
        }
    }

    @GetMapping("/getUserById")
    public AjaxResult getUserById(String userId) {
        ImgUser userById = userService.getImgUserById(userId);
        return success(userById);
    }

    /**
     * @param phone
     * @Author Lx
     * @Description 判断手机号是否已经存在
     * @Date 2024/11/19 9:04
     * @Return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("checkPhoneExist")
    public AjaxResult getPhone(String phone) {
        return success(userService.getPhone(phone));
    }

    /**
     * @param user
     * @Author Lx
     * @Description 修改用户资料的方法
     * @Date 2024/11/19 13:42
     * @Return com.ruoyi.common.core.domain.AjaxResult
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody ImgUser user) {
            userService.updatePhoneByUserId(user);
            return success();
    }
     /**
    * @Author 帅
    * @Description 使用手机号查询头像地址
    * @Date 2024/11/22 14:17
    * @Return 头像地址
    * @param userId 用户手机号
    */
     @GetMapping("/getUserAvatarUrl")
    public AjaxResult getUserAvatarUrl(String userId) {
        String userAvatarUrl = userService.getUserAvatarUrl(userId);
        return success(userAvatarUrl);
    }

    @GetMapping("/checkUserIsMerchant")
    public AjaxResult checkUserIsMerchant(String userId) {
        boolean b = userService.checkUserIsMerchant(userId);
        return success(b);
    }

}
